﻿
local RaidFramesPlus = CreateFrame("Frame") 

--- Raidwide toggle status for Debuff/Buff display
local RaidFramesPlus_ShowBuffs = 0 

--- a function table that holds tokens that will be replaced by its corresponding healthvalue text
local RaidFramesPlus_ReplaceTable = { 
 ["$abs"] = function(UnitId)
	return UnitHealth(UnitId)
end,

 ["$max"] = function(UnitId) 
	return UnitHealthMax(UnitId)
end,

 ["$dif"] = function(UnitId) 
	return UnitHealth(UnitId) - UnitHealthMax(UnitId) 
end,

 ["$abk"] =  function(UnitId) 
    local value = UnitHealth(UnitId)
	if value > 1000 then
		return string.format("%.1fk", value/1000)
	else
		return value
	end
end,
 ["$mak"] =  function(UnitId) 
    local value = UnitHealthMax(UnitId)
	if value > 1000 then
		return string.format("%.1fk", value/1000)
	else
		return value
	end
end,
 ["$difk"] =  function(UnitId) 
    local value = UnitHealthMax(UnitId) - UnitHealth(UnitId)
	if value > 1000 then
		return string.format("-%.1fk", value/1000)
	else
		return value
	end
end,
}

--- Color the fontstring with a suitable color that indicate the threatstate of its unit
-- @param button a fontstring that should be colorized
-- @param unit a valid unitid-token to get its threatsituation
-- @see RaidFramesPlus_Update
function RaidFramesPlus_UnitThreatSituationUpdate(button, unit)
	local threatState = UnitThreatSituation(unit)
	-- orange color for gaining aggro
	if threatState == 1 then
		button:SetVertexColor(1, 0.75, 0, 1)
	-- yellow color for losing aggro (but tanking)
	elseif threatState == 2 then
		button:SetVertexColor(1, 0.5, 0, 1)
	-- red color for tanking
	elseif threatState == 3 then
		button:SetVertexColor(1, 0, 0, 1)
	-- whie color for not tanking or aggroing at all 
	else
		button:SetVertexColor(1, 1, 1, 1)
	end
end

--- Set a texture next to the unitframe to its raidtarget icon or hide it if not applicable
-- @param button a texture that will be manipulated by SetRaidTargetIconTexture
-- @param unit a valid unitid-token to get its raidtarget icon
-- @see RaidFramesPlus_Update
function RaidFramesPlus_RaidTargetUpdate(button, unit)
	if RaidFramesPlus_ShowRaidTargetIcons then
		local iconIndex = GetRaidTargetIndex(unit)
		if iconIndex then
			SetRaidTargetIconTexture(button, iconIndex)
			button:Show()
		else
			button:Hide()
		end
	else
		button:Hide()
	end
end

--- Color the buttonbackground with the debuff color gotten by RaidFramesPlus_GetDebuffColor
-- @param button a button that should be colorized
-- @param unit a valid unitid-token to get color of its latest debuff
-- @see RaidFramesPlus_Update
-- @see RaidFramesPlus_GetDebuffColor
function RaidFramesPlus_UnitAura(button, unit)
	button:SetVertexColor(RaidFramesPlus_GetDebuffColor(unit))
end

--- Set the fontstringtext to the unit health
-- @param button the fontstring that should be manipulated
-- @param unit a valid unitid-token that is used to get its health value
-- @see RaidFramesPlus_Update
-- @see RaidFramesPlus_GetHealthValue
function RaidFramesPlus_UnitHealth(button, unit)
	if RaidFramesPlus_Health ~= 0 then
		button:SetText(RaidFramesPlus_GetHealthValue(unit))
	else
		button:SetText("")
	end
end

--- Scale the given button by the value stored in RaidFramesPlus_Scale
-- @param button a button that should be scaled
-- @param unit not in use
-- @see RaidFramesPlus_Update
function RaidFramesPlus_ScaleButton(button, unit)
	button:GetParent():SetScale((RaidFramesPlus_Scale or 1))
end

--- Double healthbar height if there is no mana / energy / rage / runepower / focus
--  @param button a unitbutton that should be manipulated
--  @param unit a valid unitid-token to get the powertype of its unit
--  @see RaidFramesPlus_Update
function RaidFramesPlus_DoubleHealthBar(button, unit)
	local health = button:GetName().."HealthBar"
	local mana = button:GetName().."ManaBar"
	if not InCombatLockdown() then
		-- mode 1 : show only the manabar if the unit has mana as powersource
		if RaidFramesPlus_HealthBarSize == 1 and UnitPowerType(unit) == 0 then 
			-- defaultsize of the frame is 8
			_G[health]:SetHeight(6)
			_G[health.."Frame"]:SetHeight(8)
			-- reset the texture coordinate to the first 1/4
			_G[health.."Frame"]:SetTexCoord(0,0.75,0,0.25)
			-- show the mana bar and frameing
			_G[mana]:SetHeight(6)
			_G[mana.."Frame"]:SetHeight(8)
			-- if the fontstring that represent the health of this unit exists set it between health- and mana-bar
			if _G[button:GetName().."Health"] then
				_G[button:GetName().."Health"]:SetPoint("CENTER", _G[health], "BOTTOM", 0, 0)
			end
		-- mode -1: show allways manabar
		elseif RaidFramesPlus_HealthBarSize == -1 then
			_G[health]:SetHeight(6)
			_G[health.."Frame"]:SetHeight(8)
			_G[health.."Frame"]:SetTexCoord(0,0.75,0,0.25)
			_G[mana]:SetHeight(6)
			_G[mana.."Frame"]:SetHeight(8)
			if _G[button:GetName().."Health"] then
				_G[button:GetName().."Health"]:SetPoint("CENTER", _G[health], "BOTTOM", 0, 0)
			end
		-- mode 2 (and 1 if the unit has no mana): Show allways the healthbar as doublesized
		else
			_G[health]:SetHeight(14)
			_G[health.."Frame"]:SetHeight(16)
			_G[health.."Frame"]:SetTexCoord(0,0.75,0.5,1)
			_G[mana]:SetHeight(1)
			_G[mana.."Frame"]:SetHeight(1)
			-- if the fontstring that represent the health of this unit exists set into center of the healthbar
			if _G[button:GetName().."Health"] then
				_G[button:GetName().."Health"]:SetPoint("CENTER", _G[health], "CENTER", 0, 0)
			end
		end
	end
end

--- this function adds 2 textures and 1 fontstring to the given button
--  @param button a button that should be "enhanced"
--  @param unit a valid unitid-token to reflect those 3 additions the condition of its unit
--  @see RaidFramesPlus_Update
function RaidFramesPlus_EnhanceButton(button, unit)
	local button = button:GetName()
	local background = _G[button.."Debuff"]
	local fontString = _G[button.."Health"]
	local targetIcon = _G[button.. "RaidTargetIcon"]
	if not background then
		background = _G[button]:CreateTexture(button.."Debuff", "BACKGROUND")
		background:SetTexture("Interface\\AddOns\\RaidFramesPlus\\DebuffBorder")
		background:SetBlendMode("ADD")
		background:SetWidth(82)
		background:SetHeight(36)
		background:SetPoint("TOPLEFT", _G[button], "TOPLEFT", -6, 6)
		background:SetTexCoord(0.0, 0.640625, 0, 0.625)
		background:SetVertexColor(RaidFramesPlus_GetDebuffColor(btn.unit))
	end
	if not fontString then
		fontString = _G[button.."HealthBar"]:CreateFontString(button.."Health","OVERLAY")
		fontString:SetFontObject("NumberFontNormalSmall")
		fontString:SetPoint("CENTER", _G[button.."HealthBar"], "BOTTOM", 0, 0)
		fontString:SetText(RaidFramesPlus_GetHealthValue(btn.unit))
			
	end
	if not targetIcon then
		targetIcon = _G[button]:CreateTexture(button.. "RaidTargetIcon", "OVERLAY")
		targetIcon:SetPoint("TOPRIGHT", _G[button.."HealthBar"], "TOPLEFT", 0,0)
		targetIcon:SetWidth(18)
		targetIcon:SetHeight(18)
		targetIcon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
				
		local iconIndex = GetRaidTargetIndex(btn.unit)
				
		if iconIndex then
			SetRaidTargetIconTexture(targetIcon, iconIndex)
			targetIcon:Show()
		else
			targetIcon:Hide()
		end	
	end
--	RaidFramesPlus_DoubleHealthBar(_G[button], unit)
	
end

--- This function acts as an invoker to update functions that are registered in the EventHandler
--  it iterate over all RaidPulloutFrames and for each button that .unit field is equal unit invoke the
--  callback function func with (object..object_suffix) and (unit)
--	@param object_suffix the name of object that will be used for the function-callback
--  @param func a function that will be called with the object and unit
--  @param unit a valid unitid-token
function RaidFramesPlus_Update(object_suffix, func, unit)
	-- if there are Raid Pullout Frames
	if NUM_RAID_PULLOUT_FRAMES then
		for pullout = 1, NUM_RAID_PULLOUT_FRAMES do
			 local button = 0
			 -- iterate over all buttons that are associated to the current Pullout
			 while(_G["RaidPullout"..pullout.."Button"..button+1] ~= nil) do
				button = button +1
				if unit == nil or unit == _G["RaidPullout"..pullout.."Button"..button].unit then
					-- get the object that should be updated
					local object = _G["RaidPullout"..pullout.."Button"..button..object_suffix]
					-- and if the object exists call func with the object and unit of this unitButton
					if object ~= nil then
						func(object, _G["RaidPullout"..pullout.."Button"..button].unit)
					end
				end
			end
		end
	end
end

--- this function returns the color of the latest debuff from a Unit described by UnitId
--  @param UnitId a valid unitid-token to get the latest debuff from
--  @returns r, g, b, a which represents the color of the latest debuff (0 .. 1)
function RaidFramesPlus_GetDebuffColor(UnitId)
	-- color values
	local r,g,b,a
	-- should be self explainating
	local latestDebuffType
	-- if the blizzard config variable showDispelDebuffs is set true then filter out Auras that are only
	-- dispellable
	if GetCVarBool("showDispelDebuffs") then
		-- retrieve the Debuff Type of the first (filtered) Debuff
		latestDebuffType = select(5, UnitAura(UnitId, 1, "HARMFUL|RAID"))
	else
		-- retrieve the Debuff Type of the first Debuff
		latestDebuffType = select(5, UnitAura(UnitId, 1, "HARMFUL"))
	end
	if RaidFramesPlus_DebuffColorList then
		for name, color in pairs(RaidFramesPlus_DebuffColorList) do
			if UnitBuff(UnitId, name) then
				return unpack(RaidFramesPlus_DebuffColorList[name])
			end
			if UnitDebuff(UnitId, name) then
				return unpack(RaidFramesPlus_DebuffColorList[name])
			end
		end
		
	end
	-- if there is a retrieved Debufftype lookup set the color values to the Debuff-color
	if latestDebuffType ~= nil and customColor == nil then
		r = DebuffTypeColor[latestDebuffType].r
		g = DebuffTypeColor[latestDebuffType].g
		b = DebuffTypeColor[latestDebuffType].b
		a = 1
	-- otherwise set the color values to a black transparent color
	else
		r = 0
		g = 0
		b = 0
		a = 0
	end
	-- return the gained color values
	return r,g,b,a
end

--- this function change the layout of all raidpullouts depending on RaidFramesPlus_IsVerticalLayout
--  and set thier background transperency by a value stored RaidFramesPlus_Alpha or 1
function RaidFramesPlus_LayoutManager()
	
	-- those manipulations can only be done outside of combat
	if not InCombatLockdown() then
		-- if the SavedVariable indicate that the RaidFrames should be laied out vertical so do it then
		if RaidFramesPlus_IsVerticalLayout then
			RaidFramesPlus_VerticalAlignment()
		else
			RaidFramesPlus_HorizontalAlignment()
		end
		-- For Every RaidFrame background set the alpha value
		if NUM_RAID_PULLOUT_FRAMES then
			for i = 1, NUM_RAID_PULLOUT_FRAMES do
				-- if the alpha is set (even with its default values) set the Alpha of the Background
				if RaidFramesPlus_Alpha then
					_G["RaidPullout"..i.."MenuBackdrop"]:SetAlpha(RaidFramesPlus_Alpha)
				end
			end
		end
	end
end

--- this function will arrange all raidpullout frames to horizontal alignment
function RaidFramesPlus_VerticalAlignment()
	if NUM_RAID_PULLOUT_FRAMES then
		for pullout = 1, NUM_RAID_PULLOUT_FRAMES do
			 local button = 0
			 while(_G["RaidPullout"..pullout.."Button"..button+1] ~= nil and _G["RaidPullout"..pullout.."Button"..button+1]:IsShown()) do
				button = button +1
					if button == 1 then
						_G["RaidPullout"..pullout.."Button"..button]:ClearAllPoints()	
						_G["RaidPullout"..pullout.."Button"..button]:SetPoint("TOPLEFT", _G["RaidPullout"..pullout], "TOPLEFT", 8, -10)
					else
						_G["RaidPullout"..pullout.."Button"..button]:ClearAllPoints()	
						_G["RaidPullout"..pullout.."Button"..button]:SetPoint("LEFT", _G["RaidPullout"..pullout.."Button"..button-1], "RIGHT", 8, 0)
						_G["RaidPullout"..pullout]:SetWidth((69 + 8) * button + 10)
						_G["RaidPullout"..pullout]:SetHeight((28 + 14))
						
						
					end
					if _G["RaidPullout"..pullout.."Button"..button.."RaidTargetIcon"] then
						_G["RaidPullout"..pullout.."Button"..button.."RaidTargetIcon"]:ClearAllPoints()
						_G["RaidPullout"..pullout.."Button"..button.."RaidTargetIcon"]:SetPoint("TOP", _G["RaidPullout"..pullout.."Button"..button], "TOP", 0, 0)
					end
					
			end
			if not _G["RaidPullout"..pullout].SetHeightOrg then
				_G["RaidPullout"..pullout].SetHeightOrg = _G["RaidPullout"..pullout].SetHeight
			end
			_G["RaidPullout"..pullout].SetHeight = function(value) end
						
		end
	end
end

--- this function will restore all raidpullout frames to vertical alignment
function RaidFramesPlus_HorizontalAlignment()
	if NUM_RAID_PULLOUT_FRAMES then
		for pullout = 1, NUM_RAID_PULLOUT_FRAMES do
			 local button = 0
			 while(_G["RaidPullout"..pullout.."Button"..button+1] ~= nil) do
				button = button +1
				if button == 1 then
					_G["RaidPullout"..pullout.."Button"..button]:ClearAllPoints()	
					_G["RaidPullout"..pullout.."Button"..button]:SetPoint("TOP", _G["RaidPullout"..pullout], "TOP", 1, -10);
				else
					_G["RaidPullout"..pullout.."Button"..button]:ClearAllPoints()	
					_G["RaidPullout"..pullout.."Button"..button]:SetPoint("TOP", _G["RaidPullout"..pullout.."Button"..button-1], "BOTTOM", 0, -8);
				end
				if _G["RaidPullout"..pullout.."Button"..button.."RaidTargetIcon"] then
					_G["RaidPullout"..pullout.."Button"..button.."RaidTargetIcon"]:ClearAllPoints()
					_G["RaidPullout"..pullout.."Button"..button.."RaidTargetIcon"]:SetPoint("TOPRIGHT", _G["RaidPullout"..pullout.."Button"..button.."HealthBar"], "TOPLEFT", 0,0)
				end
			end
			_G["RaidPullout"..pullout]:SetWidth(92)
			_G["RaidPullout"..pullout].SetHeight = _G["RaidPullout"..pullout].SetHeightOrg		
			RaidPullout_Update(_G["RaidPullout"..pullout])
		end
	end
end

---  this function adds newly added RaidPullouts (and buttons) to scoping-tables indexed with 
--   UnitId. Also it colorize Partymember names by thier class color if flag 
--   RaidFramesPlus_ColorizeNames is set) and hide TargetFrameNameBackground for better reading 
--   the class-colored Targetname if flag RaidFramesPlus_HideTargetFrameNameBackground is set
function RaidFramesPlus_RaidRosterUpdate()

	local pullout = 0
	while(_G["RaidPullout"..pullout+1] ~= nil) do
		-- there is a next RaidPullout so lets use it
		pullout = pullout + 1
		-- now we check for RaidButtons
		local button = 0
		while(_G["RaidPullout"..pullout.."Button"..button+1] ~= nil) do
			-- there is a next RaidPulloutButton so lets use it
			button = button + 1
			btn = _G["RaidPullout"..pullout.."Button"..button]
			
			RaidFramesPlus_EnhanceButton(btn, btn.unit)
			
			-- store the position of the first Aura Texture to repositionate it later
			 _G["RaidPullout"..pullout.."Button"..button.."Aura1"].orgPoint = _G["RaidPullout"..pullout.."Button"..button.."Aura1"].orgPoint or { _G["RaidPullout"..pullout.."Button"..button.."Aura1"]:GetPoint()}
			-- store the original functions so they can be used later
			_G["RaidPullout"..pullout.."Button"..button.."Aura2"].show1 = _G["RaidPullout"..pullout.."Button"..button.."Aura2"].show1 or _G["RaidPullout"..pullout.."Button"..button.."Aura2"].Show
			_G["RaidPullout"..pullout.."Button"..button.."Aura3"].show1 = _G["RaidPullout"..pullout.."Button"..button.."Aura3"].show1 or _G["RaidPullout"..pullout.."Button"..button.."Aura3"].Show
			_G["RaidPullout"..pullout.."Button"..button.."Aura4"].show1 = _G["RaidPullout"..pullout.."Button"..button.."Aura4"].show1 or _G["RaidPullout"..pullout.."Button"..button.."Aura4"].Show


			 if RaidFramesPlus_CompactPullouts == 1 then
				-- pull the first Aura Texture into the RaidFrame
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:ClearAllPoints()
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:SetPoint("BOTTOMRIGHT", 
					_G["RaidPullout"..pullout.."Button"..button.."HealthBar"], "TOPRIGHT", 0, 2)
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:SetFrameLevel(500)
				-- everyelse Aura Texture will be hidden
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"]:Hide()
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"].Show = function() end
				
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"]:Hide()
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"].Show = function() end
				
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"]:Hide()
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"].Show = function() end
				
				-- justify Names left (CT_RAID STYLE)
				_G["RaidPullout"..pullout.."Button"..button.."Name"]:SetJustifyH("LEFT")
			else
				-- repositionate the first Aura Textures according stored position in orgPoint
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:ClearAllPoints()
				_G["RaidPullout"..pullout.."Button"..button.."Aura1"]:SetPoint(unpack(_G["RaidPullout"..pullout.."Button"..button.."Aura1"].orgPoint))
				
				-- restore the Show functions and Show the Aura Textures
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"].Show = _G["RaidPullout"..pullout.."Button"..button.."Aura2"].show1
				_G["RaidPullout"..pullout.."Button"..button.."Aura2"]:Show()
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"].Show = _G["RaidPullout"..pullout.."Button"..button.."Aura2"].show1
				_G["RaidPullout"..pullout.."Button"..button.."Aura3"]:Show()
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"].Show = _G["RaidPullout"..pullout.."Button"..button.."Aura3"].show1
				_G["RaidPullout"..pullout.."Button"..button.."Aura4"]:Show()

				-- justify the Names to the center
				_G["RaidPullout"..pullout.."Button"..button.."Name"]:SetJustifyH("CENTER")
			end
			
			-- fix borders
			_G["RaidPullout"..pullout.."Button"..button]:SetFrameLevel(_G["RaidPullout"..pullout]:GetFrameLevel()+1)
			_G["RaidPullout"..pullout.."Button"..button.."HealthBar"]:GetStatusBarTexture():SetDrawLayer("BORDER")
			_G["RaidPullout"..pullout.."Button"..button.."ManaBar"]:GetStatusBarTexture():SetDrawLayer("BORDER")
			_G["RaidPullout"..pullout.."Button"..button.."HealthBarFrame"]:SetDrawLayer("ARTWORK")
			_G["RaidPullout"..pullout.."Button"..button.."ManaBarFrame"]:SetDrawLayer("ARTWORK")
			if _G["RaidPullout"..pullout.."Button"..button.."Target"] then
				 _G["RaidPullout"..pullout.."Button"..button.."TargetFrame"]:SetDrawLayer("ARTWORK")
			end		
			if _G["RaidPullout"..pullout.."Button"..button.."Name"] then
				_G["RaidPullout"..pullout.."Button"..button.."Name"]:SetParent(_G["RaidPullout"..pullout.."Button"..button.."HealthBar"])
			end
			
		end
	end
end

--- this function returns a healthvalue that represents the health of given unit
--  @param UnitId a valid unitid-token to determine its health
--  @returns string that represents the health
function RaidFramesPlus_GetHealthValue(UnitId)
	-- initialize string value so it can be concatenated properbly
	local value = ""
	-- absolute current health value
	if RaidFramesPlus_Health == 1 then 
		value = UnitHealth(UnitId)
	-- absolute current health value in percent (followed by a %-sign)
	elseif RaidFramesPlus_Health == 2 then 
		value = string.format("%.f%%", UnitHealth(UnitId) / UnitHealthMax(UnitId) * 100)
	-- current health in thousands (if over a thousand, followed by a 'k')
	elseif RaidFramesPlus_Health == 3 then
		value = UnitHealth(UnitId)
		if value > 1000 then 
			value = math.ceil(value / 1000) .. "k"
		end
	-- Deficite value between maximum and current health
	elseif RaidFramesPlus_Health == 4 then 
		value = UnitHealth(UnitId) - UnitHealthMax(UnitId)
	-- custom formatting
	elseif RaidFramesPlus_Health == 5 and RaidFramesPlus_CustomFormat ~= nil then 
		value = RaidFramesPlus_CustomFormat
		 -- see it function table at the beginning of this file
		for key, func in pairs(RaidFramesPlus_ReplaceTable) do
			-- replace founded func token with the function-return value
			value = value:gsub(key, func(UnitId))
		 
		end
	-- custom formatting but without a format string
    elseif RaidFramesPlus_Health == 5 and RaidFramesPlus_CustomFormat == nil then
		RaidFramesPlus_CustomFormat = "$abk($mak)"
		value = RaidFramesPlus_CustomFormat
		for key, func in pairs(replaceTable) do
		
		 value = value:gsub(key, func(UnitId))
		 
		end
	-- zero health format thus leading to an empty string
	elseif RaidFramesPlus_Health == 0 then
		value = ""
	end
	return value
end

--- this function mass toggle the display of buffs/debuffs on all Raidpullouts
function RaidFramesPlus_ToggleBuffs(button, unit)
	if RaidFramesPlus_ShowBuffs then
		button.showBuffs = 1
	else
		button.showBuffs = nil
	end
end 

--- this function parse the given arguments that passed to the slashcommand and set those settings
--  @param argv the argumentstring that should be parsed
function RaidFramesPlus_ParseSlashCommand(argv)
	-- toggle buff/debuff display via "commandline"-argument -d
	if string.find(string.lower(argv), "-d") then
		if RaidFramesPlus_ShowBuffs then
			RaidFramesPlus_ShowBuffs = nil
		else
			RaidFramesPlus_ShowBuffs = 1
		end

	end
end 

--- this function acts as DefaultVariables-Loader / SavedVariables-Loader
function RaidFramesPlus_VariablesLoaded()
	-- nil values will be created with thier default values
	if RaidFramesPlus_Health == nil then
		RaidFramesPlus_Health = 1
	end
	if RaidFramesPlus_CompactPullouts == nil then
		RaidFramesPlus_CompactPullouts = 1
	end
	if RaidFramesPlus_ShowRaidTargetIcons == nil then
		RaidFramesPlus_ShowRaidTargetIcons = 1
	end
	if RaidFramesPlus_Scale == nil then
		RaidFramesPlus_Scale = 1.0
	end
	if RaidFramesPlus_Alpha == nil then
		RaidFramesPlus_Alpha = 0.75
	end
	if RaidFramesPlus_HealthBarSize == nil then
		RaidFramesPlus_HealthBarSize = 1
	end
	if RaidFramesPlus_DebuffColorList == nil then
		RaidFramesPlus_DebuffColorList  = {}
		RaidFramesPlus_DebuffColorList["Gabe der Wildnis"] = {250/255, 154/255, 226/255, 1}
		RaidFramesPlus_DebuffColorList["Mal der Wildnis"] = {250/255, 154/255, 226/255, 1}
	end
end 

--- this function is the Addons OnEvent Handle function that delegate events to invoke other functions 
--  @param self represents the addonframe
--  @param event the name of an event that is fired
--  @param arg1 the first argument of the event (mostly unit)
function RaidFramesPlus_EventHandler(self, event, arg1, arg2, arg4, arg5, arg6)
	if event == "UNIT_THREAT_SITUATION_UPDATE" then
		RaidFramesPlus_Update("Health", RaidFramesPlus_UnitThreatSituationUpdate, arg1)
	elseif event == "UNIT_HEALTH" then
		RaidFramesPlus_Update("Health", RaidFramesPlus_UnitHealth, arg1)
		RaidFramesPlus_Update("", RaidFramesPlus_DoubleHealthBar)
	elseif event == "RAID_ROSTER_UPDATE" then
		RaidFramesPlus_Update("", RaidFramesPlus_DoubleHealthBar)
		RaidFramesPlus_Update("RaidTargetIcon", RaidFramesPlus_RaidTargetUpdate)
		RaidFramesPlus_RaidRosterUpdate()
	elseif event == "UNIT_AURA" then
		RaidFramesPlus_Update("Debuff", RaidFramesPlus_UnitAura, arg1)
	elseif event == "PLAYER_ENTERING_WORLD" then
		RaidFramesPlus_VariablesLoaded()
		RaidFramesPlus_RaidRosterUpdate()
		RaidFramesPlus_Update("", RaidFramesPlus_ScaleButton)
		RaidFramesPlus_Update("RaidTargetIcon", RaidFramesPlus_RaidTargetUpdate)
	elseif event == "RAID_TARGET_UPDATE" then
		RaidFramesPlus_Update("RaidTargetIcon", RaidFramesPlus_RaidTargetUpdate)
	elseif event == "ADDON_LOADED" and arg1 == "RaidFramesPlus" then
		RaidFramesPlus_IsEnabled = true
		RaidFramesPlus:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE")
		RaidFramesPlus:RegisterEvent("UNIT_HEALTH")
		RaidFramesPlus:RegisterEvent("RAID_ROSTER_UPDATE")
		RaidFramesPlus:RegisterEvent("PLAYER_TARGET_CHANGED")
		RaidFramesPlus:RegisterEvent("PARTY_MEMBER_CHANGED")
		RaidFramesPlus:RegisterEvent("UNIT_AURA")
		RaidFramesPlus:RegisterEvent("RAID_TARGET_UPDATE")
		RaidFramesPlus:RegisterEvent("PLAYER_ENTERING_WORLD")
		RaidFramesPlus_CreateOptionPanel()
	elseif event == "COMBAT_LOG_EVENT_UNFILTERED" and arg2 == "UNIT_DIED" then
		RaidFramesPlus_Update("RaidTargetIcon", RaidFramesPlus_RaidTargetUpdate)
	end
end



RaidFramesPlus:SetScript("OnEvent", RaidFramesPlus_EventHandler)
RaidFramesPlus:RegisterEvent("ADDON_LOADED")


		
SlashCmdList["RAIDFRAMESPLUS"] = RaidFramesPlus_ParseSlashCommand
SLASH_RAIDFRAMESPLUS1 = "/rfp"